package com.kika.obwiki.controller;

import com.kika.obwiki.rep.CategoryQueryReq;
import com.kika.obwiki.rep.CategorySaveReq;
import com.kika.obwiki.resp.CommonResp;
import com.kika.obwiki.resp.CategoryQueryResp;
import com.kika.obwiki.resp.PageResp;
import com.kika.obwiki.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    @GetMapping("/all")
    public CommonResp all() {
        CommonResp<List<CategoryQueryResp>> resp = new CommonResp<>();
        resp.setSuccess(true);
        resp.setMessage("查询成功");
        resp.setContent(categoryService.all());
        return resp;
    }

    @GetMapping("/list")
    public CommonResp list(@Valid CategoryQueryReq req) {
        CommonResp<PageResp<CategoryQueryResp>> resp = new CommonResp<>();
        resp.setSuccess(true);
        resp.setMessage("查询成功");
        resp.setContent(categoryService.listByname(req));
        return resp;
    }

    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody CategorySaveReq req) {
        CommonResp resp = new CommonResp<>(true, "成功", null);
        categoryService.save(req);
        return resp;
    }

    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable Long id) {
        CommonResp resp = new CommonResp<>(true, "删除成功", null);
        categoryService.delete(id);
        return resp;
    }
}